package com.wangdao.wechatmall.controller.wx;

import com.wangdao.wechatmall.bean.bo.wx.WXCouponBO;
import com.wangdao.wechatmall.bean.bo.wx.WXCouponDTO;
import com.wangdao.wechatmall.bean.vo.admin.BaseRespVo;
import com.wangdao.wechatmall.bean.vo.wx.WXAddordeleteFailVO;
import com.wangdao.wechatmall.bean.vo.wx.WXCouponReturnVO;
import com.wangdao.wechatmall.bean.vo.wx.WXCouponSelectListVO;
import com.wangdao.wechatmall.bean.vo.wx.WXCouponVO;
import com.wangdao.wechatmall.service.admin.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * WXCouponController
 * created by feng on 7/12/21
 */

@RestController
@RequestMapping("wx/coupon")
public class WXCouponController {

    @Autowired
    CouponService couponService;

    @GetMapping("mylist")
    public BaseRespVo list(@RequestParam("status") Integer status, @RequestParam("page") Integer page, @RequestParam("size") Integer size) {

        List<WXCouponVO> wXCouponVOList = couponService.myList(status, page, size);

        WXCouponReturnVO data = new WXCouponReturnVO();
        data.setData(wXCouponVOList);
        data.setCount(wXCouponVOList.size());

        return BaseRespVo.ok(data);
    }

    //http://182.92.235.201:8083/wx/coupon/exchange
    @PostMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map map) {
        String code = (String) map.get("code");

        Integer affectedRow = couponService.exchange(code);

        if (affectedRow == 404){
           return BaseRespVo.fail("兑换码错误");
        }

        if (affectedRow == 0) {
            return BaseRespVo.fail("优惠码已兑换");
        }

        return BaseRespVo.ok("成功");
    }

    //http://182.92.235.201:8083/wx/coupon/receive
    @PostMapping("receive")
    public Object receive(@RequestBody Map map) {
        Integer couponId = (Integer) map.get("couponId");

        Integer affectedRow = couponService.receive(couponId.toString());

       if(affectedRow == -1) {
           return WXAddordeleteFailVO.fail();
         }

        if (affectedRow == 0) {
            return BaseRespVo.fail("优惠卷已获得");
        }

        return BaseRespVo.ok("领取优惠券成功");
    }

    //http://182.92.235.201:8083/wx/coupon/selectlist?cartId=0&grouponRulesId=0
    @GetMapping("selectlist")
    public BaseRespVo selectList(@RequestParam("cartId") Integer cartId, @RequestParam("grouponRulesId") Integer grouponRulesId) {
        List<WXCouponSelectListVO> wxCouponSelectListVOList = couponService.selectList(cartId, grouponRulesId);

        return BaseRespVo.ok(wxCouponSelectListVOList);
        //Integer affectedRow = couponService.selectList(cartId, grouponRulesId);
    }

    //http://192.168.0.103:8083/wx/coupon/list?page=1&size=10
    @GetMapping("list")
    public BaseRespVo list(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        List<WXCouponDTO> wxCouponDTOList = couponService.list(page, size);

        int count = wxCouponDTOList.size();

        WXCouponBO wxCouponBO = new WXCouponBO();
        wxCouponBO.setData(wxCouponDTOList);
        wxCouponBO.setCount(count);

        return BaseRespVo.ok(wxCouponBO);
    }
}